package com.lzs.chuku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzs.chuku.service.OutorderService;
import com.lzs.entity.Outorder;
import com.lzs.entity.OutorderDetail;
import com.lzs.entity.Review;
import com.lzs.entity.ReviewDetail;
import com.lzs.chuku.feign.KucunFeign;
import com.lzs.chuku.mapper.OutorderDetailMapper;
import com.lzs.chuku.mapper.ReviewDetailMapper;
import com.lzs.chuku.mapper.ReviewMapper;
import com.lzs.chuku.service.ReviewService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzs.util.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘中帅
 * @since 2020-11-23
 */
@Service
public class ReviewServiceImpl extends ServiceImpl<ReviewMapper, Review> implements ReviewService {

    @Autowired
    private KucunFeign kucunFeign;
    @Autowired
    private ReviewDetailMapper reviewDetailMapper;
    @Autowired
    private OutorderDetailMapper outorderDetailMapper;
    @Autowired
    private ReviewMapper reviewMapper;
    @Autowired
    private OutorderService outorderService;
    @Override
    public R addReview(Review review) {
        try{
            QueryWrapper<Review> wrapper=new QueryWrapper<>();
            wrapper.eq("outorderno",review.getOutorderno());
            Review review1 = reviewMapper.selectOne(wrapper);
            if (review1!=null){
                return R.error().message("改出库订单已生成出库复核，请勿再次提交");
            }else{
                review.setState("1");
                reviewMapper.insert(review);
            }
            return R.ok().message("新增成功");
        }catch (RuntimeException e){
            return R.error().message("新增失败");
        }
    }

    @Override
    public R findByPageReview(Review review, Integer currentPage, Integer pageSize) {
        try {
            String outorderno=review.getOutorderno();
            String state=review.getState();
            QueryWrapper<Review> wrapper=new QueryWrapper<>();
            if (StringUtils.isNotEmpty(outorderno)){
                wrapper.like("outorderno",outorderno);
            }
            if (StringUtils.isNotEmpty(state)){
                wrapper.eq("state",state);
            }
            Page<Review> page=new Page<>(currentPage,pageSize);
            reviewMapper.selectPage(page,wrapper);
            return R.ok().message("查询成功").data("page",page);
        }catch (RuntimeException e){
            return R.error().message("查询失败");
        }
    }

    @Override
    public R orveReview(Long reviewid) {
        try {
            Review review = reviewMapper.selectById(reviewid);
            QueryWrapper<OutorderDetail> wrapper=new QueryWrapper<>();
            wrapper.eq("pid",review.getOutorderid());
            List<OutorderDetail> outorderDetails = outorderDetailMapper.selectList(wrapper);
            if(outorderDetails.size()==0){
                return R.error().message("您还未复核商品");
            }
            QueryWrapper<ReviewDetail> wrapper1=new QueryWrapper<>();
            wrapper1.eq("pid",review.getId());
            List<ReviewDetail> reviewDetails = reviewDetailMapper.selectList(wrapper1);
            if (outorderDetails.size()==reviewDetails.size()){
                for (int i = 0; i <outorderDetails.size() ; i++) {
                    OutorderDetail outorderDetail=outorderDetails.get(i);
                    for (int j = 0; j <reviewDetails.size() ; j++) {
                        if (outorderDetail.getCommodityid()==reviewDetails.get(j).getCommodityid()){
                            if (outorderDetail.getQty()!=reviewDetails.get(j).getQty()){
                                changeState(reviewid,"4");
                                return R.error().message("复核数量与出库订单中的数量不相同，复核失败");
                            }
                        }
                    }
                }
                for (int i = 0; i <outorderDetails.size() ; i++) {
                    OutorderDetail outorderDetail=outorderDetails.get(i);
                    kucunFeign.updateRealQuantity(outorderDetail.getLocatorid(),outorderDetail.getCommodityid(),outorderDetail.getWarehouseid(),outorderDetail.getQty());
                }
                QueryWrapper<Outorder> wrapper2=new QueryWrapper<>();
                wrapper2.eq("id",review.getOutorderid());
                Outorder outorder = outorderService.getById(review.getOutorderid());
                outorder.setDepottime(new Date());
                outorder.setState("6");
                outorderService.update(outorder,wrapper2);
                changeState(reviewid,"3");
                return R.ok().message("复核成功");
            }else{
                return R.error().message("商品未审核完毕");
            }

        }catch (RuntimeException e){
            return R.error().message("复核失败");
        }
    }

    @Override
    public R changeState(Long reviewid,String state) {
        try {
            reviewMapper.changeState(reviewid,state);
            return R.ok().message("状态改变成功");
        }catch (RuntimeException e){
            return R.error().message("状态改变失败");
        }

    }
}
